<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.search" placeholder="批次|卡号" style="width: 200px;" clearable class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" :disabled="!multipleSelection || multipleSelection.length == 0" @click="handleBindReceiver()">
        领卡登记
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" :selectable="selection" />
      <el-table-column label="服务卡信息">
        <el-table-column label="卡种类" width="70px" align="center">
          <template slot-scope="{row}">
            <span>{{ row.cardCategory | cardCategoryEnum }}</span>
          </template>
        </el-table-column>
        <el-table-column label="批次" min-width="80px" prop="batchId">
          <template slot-scope="{row}">
            <span v-if="row.cardCategory === 'VIRTUAL'">-</span>
            <span v-else>{{ row.batchId }}</span>
          </template>
        </el-table-column>
        <el-table-column label="卡号" min-width="100px" prop="cardCode">
          <template slot-scope="{row}">
            <span v-if="row.cardCategory === 'VIRTUAL'">-</span>
            <span v-else>{{ row.cardCode }}</span>
          </template>
        </el-table-column>
        <el-table-column label="密码" min-width="100px">
          <template slot-scope="{row}">
            <el-tag type="warning">{{ row.cardCategory === 'VIRTUAL' ? '-' : row.cardPassword }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="生成时间" min-width="100px" align="center">
          <template slot-scope="{row}">
            <span>{{ new Date(row.createDatetime) | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="卡类型">
        <el-table-column label="卡样式" width="80px" align="center">
          <template slot-scope="{row}">
            <el-tag>{{ row.cardStyle | cardStyleEnum }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="卡名称" width="120px" prop="cardName" />
        <el-table-column label="卡次数" width="70px" prop="cardTimes" />
        <el-table-column label="卡分钟" width="70px" prop="cardMinutes" />
        <el-table-column label="卡价格" width="70px" prop="cardPrice" />
      </el-table-column>
      <el-table-column label="领卡登记">
        <el-table-column label="领卡人" width="100px">
          <template slot-scope="{row}">
            <span v-if="row.cardCategory === 'VIRTUAL'">-</span>
            <el-button v-else type="primary" size="mini" @click="handleBindReceiver(row)">
              {{ row.receiverName || '领卡人' }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="手机号" width="110px" prop="receiverMobile">
          <template slot-scope="{row}">
            <span>{{ row.cardCategory === 'VIRTUAL' ? '-' : row.receiverMobile }}</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="绑定人">
        <el-table-column label="绑定人" width="130px" prop="userName" />
        <el-table-column label="手机号" width="130px" prop="userMobile" />
        <el-table-column label="绑定时间" width="140px" align="center">
          <template slot-scope="{row}">
            <span>{{ row.boundDatetime ? new Date(row.boundDatetime) : '' | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getList" />

    <el-dialog title="领卡登记" :visible.sync="dialogBindReceiverVisible" width="500px">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-width="80px">
        <el-form-item label="领卡人">
          <el-input v-model="temp.receiverName" placeholder="领卡人" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="temp.receiverMobile" placeholder="手机号" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogBindReceiverVisible = false">取消</el-button>
        <el-button type="primary" @click="modifyData()">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, modifyServiceCard } from '@/api/service/card'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ServiceCard',
  components: { Pagination },
  directives: { waves },
  filters: {
    cardCategoryEnum(cardCategory) {
      const cardCategoryMap = {
        VIRTUAL: '虚拟卡',
        PHYSICAL: '实物卡'
      }
      return cardCategoryMap[cardCategory] || '-'
    },
    cardStyleEnum(cardStyle) {
      const cardStyleMap = {
        NORMAL: '普通卡',
        SILVER: '白银卡',
        GOLD: '黄金卡',
        PLATINUM: '铂金卡',
        BLACK: '黑金卡',
        DIAMOND: '钻石卡'
      }
      return cardStyleMap[cardStyle] || '-'
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      multipleSelection: [],
      total: 0,
      listLoading: true,
      listQuery: {
        current: 1,
        size: 20,
        search: undefined
      },
      temp: {
      },
      dialogBindReceiverVisible: false,
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total

        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    selection(row, index) {
      return row.cardCategory === 'PHYSICAL'
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleBindReceiver(row) {
      if (row) {
        this.temp = {
          cardId: row.cardId,
          receiverName: row.receiverName,
          receiverMobile: row.receiverMobile
        }
      } else {
        this.temp = {
          receiverName: '',
          receiverMobile: ''
        }
      }
      this.dialogBindReceiverVisible = true
    },
    modifyData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.temp.cardId) {
            modifyServiceCard({
              ...this.temp
            }).then(() => {
              this.getList()
              this.dialogBindReceiverVisible = false
              this.$notify({
                title: '成功',
                message: '保存成功',
                type: 'success',
                duration: 2000
              })
            })
          } else {
            Promise.all(this.multipleSelection.map(row => {
              return modifyServiceCard({
                ...this.temp,
                cardId: row.cardId
              })
            })).then(() => {
              this.getList()
              this.dialogBindReceiverVisible = false
              this.$notify({
                title: '成功',
                message: '保存成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        }
      })
    }
  }
}
</script>
